<?php

abstract class Template_Exception extends CMS_Exception {
	public function __construct($template){
		$this->_template = $template;
		parent::__construct();
	}
	public function __toString(){
		return "{$this->_template}\r\n" . parent::__toString();
	}
}
class Template_Class_Exception extends Template_Exception {
	public function __construct($template, $class){
		$this->_class = $class;
		parent::__construct($template);
	}
	public function __toString(){
		return "{$this->_class}\r\n" . parent::__toString();
	}
}
class Template_Class_Not_Found_Exception extends Template_Class_Exception {}
class Template_Class_Invalid_Exception extends Template_Class_Exception {}
class Template_Invalid_Exception extends Template_Exception {}

abstract class Template {

	public function __construct($data = array()){
		foreach($data as $k => $v){
			$this->$k = $v;
		}
	}

	public static function Create($tpl, $data){
		$file = join($tpl, '/');
		array_unshift($tpl, 'Template');
		$class = join($tpl, '_');
		CMS::File($file);
		if(!class_exists($class)){
			throw new Template_Class_Not_Found_Exception($tpl,$class);
		}
		if(!is_subclass_of($class, "Template")){
			throw new Template_Class_Invalid_Exception($tpl,$class);
		}
		$data['template'] = $tpl;
		return new $class($data);
	}

	abstract function display();


}
